//
//  CZQMasterPageView.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/4/9.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQMasterPageView.h"
#import "CZQphotoShowTableViewCell.h"
#import <MediaPlayer/MediaPlayer.h>
@interface CZQMovePhotoShowTableViewCell ()<SDPhotoBrowserDelegate>

@end
@implementation CZQMovePhotoShowTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        /**
         滚动图
         */
        UIScrollView *scrollView = [UIScrollView new];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        
        [self setConstraints];
        
        
    }
    return self;
}


-(void)setConstraints
{
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    
}

-(void)setArrM:(NSMutableArray *)arrM
{
    
    _arrM = arrM;
    
    _imageView = nil;
    
    
    if (arrM.count == 0) {
        return;
    }
    
    
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
        make.height.mas_equalTo(self.width/4 + 20);
    }];
    
    
    
    
    //擅长
    if (self.section == 3) {
        
        
        
    }else
    {
        
        
        for (UIView *view in self.scrollView.subviews) {
            
            if ([view isKindOfClass:[UIImageView class]] || [view isKindOfClass:[CZQLongBtnView class]]) {
                [view removeFromSuperview];
            }
            
        }
        
        //记录前一个view
        UIImageView *imgView = nil;
        
        for (int i = 0; i < arrM.count; i ++) {
            
            DHCustomImageView *img = [DHCustomImageView new];
            img.deleteBtn.hidden = YES;
            img.deleteBtn.tag = 500 + i;
            //            img.image = arrM[i];
            [img sd_setImageWithURL:[Helper imageUrlStringToChinese:[NSURL URLWithString:arrM[i]] imageType:nil] placeholderImage:PlaceHold];
            img.userInteractionEnabled = YES;
            [self.scrollView addSubview:img];
            img.tag = 300 + i;
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showPhoto:)];
            [img addGestureRecognizer:tap];
            
            if ([arrM[i] containsString:@"video_"]) {
                
                
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    // 处理耗时操作的代码块...
                    
                    UIImage *image = [Helper thumbnailImageForVideo:[NSURL URLWithString:arrM[i]] atTime:1];
                    //通知主线程刷新
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //回调或者说是通知主线程刷新，
                        
                        img.image = image;
                    });
                });
                
//
                
                img.image = PlaceHold;
                
                
                
                UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                
                playBtn.tag = 500 + i;
                
                //            playBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
                
                [playBtn setImage:[UIImage imageNamed:@"icon_video"] forState:UIControlStateNormal];
                
                [playBtn addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
                
                [img addSubview:playBtn];
                
                [playBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.left.mas_equalTo(img).offset(10);
                    make.bottom.right.mas_equalTo(img).offset(-10);
                }];
            }
        
            
            
            if (i == 0) {
                
                [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.left.mas_equalTo(self.scrollView).offset(10);
                    make.bottom.mas_equalTo(self.scrollView).offset(-10);
                    make.width.mas_equalTo(img.mas_height);
                    
                    make.height.mas_equalTo(self.width/4 - 20);
                    
                }];
                
            }else if (i == arrM.count - 1){
                
                [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.bottom.mas_equalTo(imgView);
                    make.left.mas_equalTo(imgView.mas_right).offset(20);
                    
                    make.width.height.mas_equalTo(imgView);
                    
                }];
                
            }else
            {
                [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.bottom.mas_equalTo(imgView);
                    make.left.mas_equalTo(imgView.mas_right).offset(20);
//                    make.right.mas_equalTo
                    make.width.height.mas_equalTo(imgView);
                    
                }];
                
            }
            imgView = img;
            
            
        }
        
        [self setNeedsLayout];
        [self setNeedsDisplay];
        
        self.scrollView.contentSize =CGSizeMake( (self.width/4 )*self.arrM.count + 20, 0);
        
    }
    
    
    
    
    
    //    self.scrollView.contentSize =CGSizeMake(50 *arrM.count + 20, 0);
    
    
}

-(void)layoutSubviews
{
    
}

-(void)setModel:(CZQResumeModel *)model
{
    
    _model = model;
    
}




- (void)showPhoto:(UITapGestureRecognizer *)tap
{
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag - 300;
    browser.sourceImagesContainerView = imageView;
    
    browser.imageCount = self.arrM.count;
    browser.delegate = self;
    [browser show];
    
//    browser.removeBtn.hidden = NO;
}

#pragma mark - 播放视频
-(void)playVideo:(UIButton *)sender
{
    
    // 设置视频播放器
    
    
    MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:self.arrM[sender.tag - 500]]];
    
    [moviePlayerController.moviePlayer prepareToPlay];
    moviePlayerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [self.viewController presentMoviePlayerViewControllerAnimated:moviePlayerController];
    
    
}

#pragma mark - 删除图片
-(void)deleteImg:(UIButton *)sender
{
    [self.arrM removeObjectAtIndex:sender.tag - 500];
    
    [self setArrM:self.arrM];
    
    if (self.arrM.count == 0) {
        self.model.Pics = @"";
    }else
    {
        self.model.Pics = [self.arrM componentsJoinedByString:@","];
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(CZQphotoShowTableViewCellRefreashData)]) {
        [_delegate CZQphotoShowTableViewCellRefreashData];
    }
}
#pragma mark - SDPhotoBrowserDelegate

-(void)photoBrowser:(SDPhotoBrowser *)browser removePhoto:(NSInteger)index
{
    [self.arrM removeObjectAtIndex:index];
    
    [self setArrM:self.arrM];
    
    if (self.arrM.count == 0) {
        self.model.Pics = @"";
    }else
    {
        self.model.Pics = [self.arrM componentsJoinedByString:@","];
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(CZQphotoShowTableViewCellRefreashData)]) {
        [_delegate CZQphotoShowTableViewCellRefreashData];
    }
    
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    //    _index = index;
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",self.arrM[index] ,mid_image];
    urlStr=[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlStr];
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    //    UIImageView *imageView ;
    
    return PlaceHold;
}

@end

@implementation CZQMasterPageView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        /**
         背景
         */
        UIImageView *bgImageView = [[UIImageView alloc] init];
        bgImageView.image = PlaceHold;
        [self addSubview:bgImageView];
        self.bgImageView = bgImageView;
        
        /**
         返回按钮
         */
        UIButton *backBtn = [[UIButton alloc] init];
        [backBtn setImage:@"icon_mback"];
        [self addSubview:backBtn];
        [backBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 10)];
        [backBtn addTarget:self action:@selector(pop)];
        self.popBtn = backBtn;
        
        /**
         师傅
         */
        UIView *typeView = [UIView new];
        typeView.backgroundColor = [ButtonBackGroundColor colorWithAlphaComponent:0.8];
        [self addSubview:typeView];
        typeView.layer.cornerRadius = 20;
        typeView.clipsToBounds = YES;
        self.typeView = typeView;
        
        UIButton *typeBtn = [[UIButton alloc] init];
        [typeBtn setTitle:@"师  傅"];
        
        [typeBtn setTitleColor:CSWhiteColor];
        [typeBtn setImage:@"icon_star_grey"];
        
        typeBtn.titleLabel.font = MidFont;
        [self.typeView addSubview:typeBtn];
        self.typeBtn = typeBtn;
        
        
        
        /**
         昵称
         */
        UILabel *nickNameLabel = [[UILabel alloc] init];
        nickNameLabel.font = BoldBigFont;
        nickNameLabel.textColor = CSWhiteColor;
        nickNameLabel.text = @"暗暗";
        [self addSubview:nickNameLabel];
        self.nickNameLabel = nickNameLabel;
        
        
        /**
         性别年龄
         */
        
        UIView *sexView = [UIView new];
        sexView.backgroundColor = [ButtonBackGroundColor colorWithAlphaComponent:0.8];
        [self addSubview:sexView];
        sexView.layer.cornerRadius = 10;
        sexView.clipsToBounds = YES;
        self.sexView = sexView;
        
        UIButton *sexBtn = [[UIButton alloc] init];
        [sexBtn setTitle:@"22"];
        [sexBtn setTitleColor:CSWhiteColor];
        [sexBtn setImage:@"icon_man"];
        
        sexBtn.titleLabel.font = MidFont;
        [self.sexView addSubview:sexBtn];
        self.sexBtn = sexBtn;
        
        
        /**
         地址|时间|ID
         */
        UILabel *adressLable = [[UILabel alloc] init];
        adressLable.font = MidBigFont;
        adressLable.textColor = CSWhiteColor;
        adressLable.text = @"地址|时间|ID";
        [self addSubview:adressLable];
        self.adressLable = adressLable;
        
        /**
         徒弟数
         */
        UILabel *prenticeLable = [[UILabel alloc] init];
        prenticeLable.font = MidBigFont;
        prenticeLable.textColor = CSWhiteColor;
        prenticeLable.text = @"徒弟：1000";
        [self addSubview:prenticeLable];
        self.prenticeLable = prenticeLable;

        
        /**
         粉丝数
         */
        UILabel *fanLable = [[UILabel alloc] init];
        fanLable.font = MidBigFont;
        fanLable.textColor = CSWhiteColor;
        fanLable.text = @"粉丝：10万";
        [self addSubview:fanLable];
        self.fanLable = fanLable;
        
        /**
         关注按钮
         */
        UIButton *attendBtn = [[UIButton alloc] init];
        attendBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        [attendBtn setTitle:@"＋关注"];
        [attendBtn setTitle:@"取消关注" forState:UIControlStateSelected] ;
        [attendBtn setTitleColor:CSWhiteColor];
        attendBtn.layer.cornerRadius = 3;
        attendBtn.clipsToBounds = YES;
        attendBtn.layer.borderWidth = 1;
        attendBtn.layer.borderColor = CSWhiteColor.CGColor;
        attendBtn.backgroundColor = [ButtonBackGroundColor colorWithAlphaComponent:0.8];
        attendBtn.titleLabel.font = MidFont;
        [self addSubview:attendBtn];
        self.attendBtn = attendBtn;
        
        [self setConstraints];
        
    }
    return self;
}
-(void)setConstraints
{
    /**
     背景
     */
    [self.bgImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
        
    }];
    
    
    
    /**
     返回按钮
     */
    [self.popBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(CZQ_StatusBarHeight);
        
        make.left.mas_equalTo(self).offset(15);
    }];

    
    /**
     师傅
     */
    [self.typeView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self).offset(-20);
        make.centerY.mas_equalTo(self).offset(-20);
        make.height.mas_equalTo(40);
        
    }];
    [self.typeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.typeView).offset(20);
        make.centerY.mas_equalTo(self.typeView);
        make.right.mas_equalTo(self.typeView).offset(-20);
    }];
    
    [self.typeBtn imageOnTheTitleLeftWithSpace:10];
    
    /**
     昵称
     */
    [self.nickNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self).offset(15);
        
    }];
    
    
    /**
     性别年龄
     */
    [self.sexView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.nickNameLabel);
        make.left.mas_equalTo(self.nickNameLabel.mas_right).offset(15);
        make.height.mas_equalTo(20);
        
    }];
    
    [self.sexBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.sexView);
        make.left.mas_equalTo(self.sexView).offset(7);
        make.right.mas_equalTo(self.sexView).offset(-7);
        
    }];
    
    [self.sexBtn imageOnTheTitleLeftWithSpace:3];
    
    /**
     地址|时间|ID
     */
    [self.adressLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.nickNameLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.nickNameLabel);
        make.bottom.mas_equalTo(self).offset(-20);
        
    }];

    
    /**
     徒弟数
     */
    [self.prenticeLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self).offset(-15);
        
    }];
    
    /**
     粉丝数
     */
    [self.fanLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.prenticeLable.mas_bottom).offset(5);
        make.right.mas_equalTo(self.prenticeLable);
        
    }];
    
    /**
     关注按钮
     */
    [self.attendBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.fanLable.mas_bottom).offset(5);
        make.right.mas_equalTo(self.prenticeLable);
        make.centerY.mas_equalTo(self.adressLable);
        
    }];
}

#pragma mark - pop
-(void)pop
{
    [self.viewController.navigationController popViewControllerAnimated:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
